<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use app\service\InnerPurchaseService;
use app\service\OuterPurchaseService;
use app\service\GoodsService;
use app\service\StoreService;
use app\service\UserService;
use app\service\StoreLossService;
use think\facade\Validate;
use think\Request;

class StoreController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
      $page = $request->param('page', 1);
      $pageSize = $request->param('pageSize', env('pager.default.size'));
      $base = $request->param('base', []);
      $count = $request->param('count', '');
      $name = $request->param('name', '');
      $categoryId = $request->param('categoryId', '');
      $conditions = [];
      if ($count) {
        $conditions[] = ['count', 0, '>'];
      }
      if($base){
        $conditions[] = ['base', $base, 'IN'];
      }
      if (strlen($name) > 0) {
        $conditions[] = ['name', '%'.$name.'%', 'like'];
      }
      if ($categoryId) {
        $conditions[] = ['category_id', $categoryId, '='];
      }
      $list = StoreService::getInstance()->getStoreByCondition($conditions);
      // 分页
      $count = count($list); //总条数
      $start=intval(($page - 1) * $pageSize); //偏移量，当前页-1乘以每页显示条数
      $pager = [
        'totalCount' => $count,
        'pageSize' => intval($pageSize),
        'currPage' => intval($page),
        'totalPage' => intval($count/$pageSize)+1
      ];
      $data = array_slice($list, $start, intval($pageSize));
      return $this->send200ResponseWithPager($data,$pager);

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
